package com.xysq.activity;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rockcent.action.CallbackListener;
import com.rockcent.model.BClientCustomerBO;
import com.rockcent.model.CustomerCouponBO;
import com.rockcent.model.constant.CustomerCouponCategory;
import com.rockcent.model.constant.RealNameAuthType;
import com.umeng.analytics.MobclickAgent;
import com.xysq.fragment.CustomerGoodsFragment;
import com.xysq.fragment.EvaluateFragment;
import com.xysq.lemon.R;
import com.xysq.util.ToastUtil;
import com.xysq.util.UserInfoKeeper;

import java.util.ArrayList;
import java.util.List;

public class CustomerCouponDetailActivity extends BaseActivity implements EvaluateFragment.OnFragmentInteractionListener, CustomerGoodsFragment.OnFragmentInteractionListener, View.OnClickListener {
    private List<Fragment> fragmentList;
    private Button buyNowBtn;
    private TextView fragmentGoodsTxt;
    private TextView fragmentEvaluateTxt;
    private ImageView fragmentGoodsImg;
    private ImageView fragmentEvaluateImg;
    private ImageView backBtn;
    private int couponModelId;
    private int customerId;
    private boolean isFavorite;
    FragmentPagerAdapter adapter;
    ViewPager pager;
    //    private LinearLayout saveLayout;
//    private LinearLayout connectsellerLayout;
    private RelativeLayout goodsIndicaterLayout;
    private RelativeLayout evaluateIndicaterLayout;
    public static final String COUPONMODEL_ID = "couponModel_Id";
    public static final String CHANNEL_ID = "channel_Id";
    public static final String CUSTOMER_ID = "customer_id";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_coupon_details);

        initView();
        processFragmentList();
    }

    private void processFragmentList() {
        fragmentList = new ArrayList<>();
        fragmentList.add(CustomerGoodsFragment.newInstance(couponModelId));
        fragmentList.add(EvaluateFragment.newInstance(couponModelId));
        adapter = new GoogleMusicAdapter(getSupportFragmentManager());

        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                Log.e("which i selected", i + "");
                if (i == 0) {
                    fragmentGoodsTxt.setTextColor(Color.RED);
                    fragmentEvaluateTxt.setTextColor(getResources().getColor(R.color.txt_white));
                    fragmentGoodsImg.setBackgroundColor(Color.RED);
                    fragmentEvaluateImg.setBackgroundColor(getResources().getColor(R.color.text_normal));
                }
                if (i == 1) {
                    fragmentGoodsTxt.setTextColor(getResources().getColor(R.color.txt_white));
                    fragmentEvaluateTxt.setTextColor(Color.RED);
                    fragmentGoodsImg.setBackgroundColor(getResources().getColor(R.color.text_normal));
                    fragmentEvaluateImg.setBackgroundColor(Color.RED);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    private void initView() {
        goodsIndicaterLayout = (RelativeLayout) findViewById(R.id.layout_goods_indicater);
        evaluateIndicaterLayout = (RelativeLayout) findViewById(R.id.layout_evaluate_indicater);
//        saveLayout = (LinearLayout) findViewById(R.id.layout_save);
//        connectsellerLayout = (LinearLayout) findViewById(R.id.layout_connect_seller);
        backBtn = (ImageView) findViewById(R.id.img_back);
        goodsIndicaterLayout.setOnClickListener(this);
        evaluateIndicaterLayout.setOnClickListener(this);
//        saveLayout.setOnClickListener(this);
//        connectsellerLayout.setOnClickListener(this);
        backBtn.setOnClickListener(this);
        couponModelId = getIntent().getIntExtra(COUPONMODEL_ID, 0);
        customerId = getIntent().getIntExtra(CUSTOMER_ID, 0);
        buyNowBtn = (Button) findViewById(R.id.btn_buy_now);
        fragmentEvaluateImg = (ImageView) findViewById(R.id.img_fragment_evaluate);
        fragmentGoodsImg = (ImageView) findViewById(R.id.img_fragment_goods);
        fragmentEvaluateTxt = (TextView) findViewById(R.id.txt_fragment_evaluate);
        fragmentGoodsTxt = (TextView) findViewById(R.id.txt_fragment_goods);
        fragmentGoodsTxt.setTextColor(getResources().getColor(R.color.text_red));
        fragmentGoodsImg.setBackgroundColor(getResources().getColor(R.color.text_red));
        buyNowBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MobclickAgent.onEvent(CustomerCouponDetailActivity.this, "1008");
                if (TextUtils.isEmpty(UserInfoKeeper.readPhoneNumber(CustomerCouponDetailActivity.this))) {
                    startActivity(new Intent(CustomerCouponDetailActivity.this, LoginActivity.class));
                } else {
                    checkIdentify();
                }
            }
        });
    }

    //获取用户身份认证状态
    private void checkIdentify() {
        appAction.getBClientCustomer(UserInfoKeeper.readPhoneNumber(this), new CallbackListener<BClientCustomerBO>() {
            @Override
            public void onSuccess(BClientCustomerBO data) {
                if (data.getIsRealnameAuth() == RealNameAuthType.UNAUTH) {//未实名认证，跳到实名认证页面
                    /*if (TextUtils.isEmpty(UserInfoKeeper.readRealName(CustomerCouponDetailActivity.this))) {
                        startActivityForResult(new Intent(CustomerCouponDetailActivity.this, UserActivity.class), UserActivity.IDENTIFY_CODE);
                        ToastUtil.showShort(CustomerCouponDetailActivity.this, getResources().getString(R.string.toast_update_info));
                    } else {*/
                    startActivity(new Intent(CustomerCouponDetailActivity.this, IdentifyActivity.class));
//                    }
                } else if (data.getIsRealnameAuth() == RealNameAuthType.FAIL) {
                    /*if (TextUtils.isEmpty(UserInfoKeeper.readRealName(WantBuyActivity.this))) {
                        startActivityForResult(new Intent(WantBuyActivity.this, UserActivity.class), UserActivity.IDENTIFY_CODE);
                        ToastUtil.showShort(WantBuyActivity.this, getResources().getString(R.string.toast_update_info));
                    } else {*/
                    Intent intent = new Intent(CustomerCouponDetailActivity.this, IdentifyActivity.class);
                    intent.putExtra(IdentifyActivity.FAIL, IdentifyActivity.FAIL);
                    startActivity(intent);
//                    }
                } else if (data.getIsRealnameAuth() == RealNameAuthType.AUTHING) {
                    ToastUtil.showShort(CustomerCouponDetailActivity.this, getResources().getString(R.string.toast_authing));
                } else if (data.getIsRealnameAuth() == RealNameAuthType.AUTH) {
                    getCustomerCouponType();
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {

            }
        });
    }

    private void getCustomerCouponType() {
        appAction.viewCustomerCoupon(couponModelId, new CallbackListener<CustomerCouponBO>() {
            @Override
            public void onSuccess(CustomerCouponBO data) {
                if (!String.valueOf(data.getCustomerId()).equals(UserInfoKeeper.readKcouponId(CustomerCouponDetailActivity.this))) {
                    String s = UserInfoKeeper.readKcouponId(CustomerCouponDetailActivity.this);
                    Intent intent = new Intent(CustomerCouponDetailActivity.this, PayActivity.class);
                    intent.putExtra(PayActivity.COUPON_MODEL_ID, Integer.valueOf(couponModelId));
                    intent.putExtra(PayActivity.COUPON_CATEGORY, CustomerCouponCategory.CLIENT);
                    startActivity(intent);
                } else {
                    ToastUtil.showShort(CustomerCouponDetailActivity.this, "自己发的券自己不能购买");
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {

            }
        });
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    public void onClick(View view) {
        if (view == goodsIndicaterLayout) {
            pager.setCurrentItem(0);
        }
        if (view == evaluateIndicaterLayout) {
            pager.setCurrentItem(1);
        }
        if (view == backBtn) {
            finish();
        }
//        if (view == saveLayout) {
//
//        }
//        if (view == connectsellerLayout) {
//            MobclickAgent.onEvent(CustomerCouponDetailActivity.this, "1010");
//            appAction.getCustomer("", "", String.valueOf(customerId), new CallbackListener<CustomerBO>() {
//                @Override
//                public void onSuccess(CustomerBO data) {
//                    String tel = data.getPhone();
//                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + tel));
//                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    startActivity(intent);
//                }
//
//                @Override
//                public void onFailure(String errorCode, String message) {
//                    ToastUtil.showShort(CustomerCouponDetailActivity.this, "获取商家电话失败");
//                }
//            });
//          /*  appAction.listCouponStore(couponModelId, new CallbackListener<List<CouponStore>>() {
//                @Override
//                public void onSuccess(List<CouponStore> data) {
//
//                }
//
//                @Override
//                public void onFailure(String errorCode, String message) {
//                    ToastUtil.showShort(CustomerCouponDetailActivity.this, "获取商家电话失败");
//                }
//            });*/
//        }
    }

    class GoogleMusicAdapter extends FragmentPagerAdapter {
        public GoogleMusicAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (position == 0) {
                return getResources().getString(R.string.txt_goods);
            }
            if (position == 1) {
                return getResources().getString(R.string.txt_evaluate);
            }
            return null;
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("技能页面");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("技能页面");
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == UserActivity.IDENTIFY_CODE) {
            boolean result = data.getBooleanExtra(UserActivity.RESULT, true);
            if (result) {//用户保存信息后返回
                startActivity(new Intent(CustomerCouponDetailActivity.this, IdentifyActivity.class));
            } else {//用户直接返回，不做任何操作
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
